package com.jdy.haoduoaiteacher.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;

import com.ycsj.goldmedalnewconcept.R;


/**
 * 六边形带进度的进度条，线程安全的View，可直接在线程中更新进度
 * 
 * @author
 * 
 */
public class HexagonProgress extends View {
  /**
   * 画笔对象的引用
   */
  private Paint mPaint, mPaint2, mPaint3;

  /**
   * 画笔路径
   */
  private Path path, mPath, mPath2;

  /**
   * 环的颜色
   */
  private int roundColor;

  /**
   * 环进度的颜色
   */
  private int roundProgressColor;

  /**
   * 中间进度百分比的字符串的颜色
   */
  private int textColor;

  /**
   * 中间进度百分比的字符串的字体
   */
  private float textSize;

  /**
   * 环的宽度
   */
  private float roundWidth;

  /**
   * 最大进度
   */
  private double max;

  /**
   * 当前进度
   */
  private double progress;
  /**
   * 是否显示中间的进度
   */
  private boolean textIsDisplayable;

  /**
   * 进度的风格，实心或者空心
   */
  private int style;

  public static final int STROKE = 0;
  public static final int FILL = 1;

  public HexagonProgress(Context context) {
    this(context, null);
  }

  public HexagonProgress(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public HexagonProgress(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    // paint = new Paint();
    mPaint = new Paint();
    mPaint3 = new Paint();

    TypedArray mTypedArray = context.obtainStyledAttributes(attrs,
        R.styleable.HexagonProgressBar);

    roundColor = mTypedArray.getColor(
        R.styleable.HexagonProgressBar_hexagonColor, Color.BLACK);

    roundColor = 0xffb58c53;

    roundProgressColor = mTypedArray.getColor(
        R.styleable.HexagonProgressBar_hexagonProgressColor, Color.GREEN);
    textColor = mTypedArray.getColor(
        R.styleable.HexagonProgressBar_textColorrr, Color.BLACK);
    textSize = mTypedArray.getDimension(
        R.styleable.HexagonProgressBar_textSizeee, 15);
    roundWidth = mTypedArray.getDimension(
        R.styleable.HexagonProgressBar_hexagonWidth, 5);
    max = mTypedArray.getInteger(R.styleable.HexagonProgressBar_maxxx, 100);
    textIsDisplayable = mTypedArray.getBoolean(
        R.styleable.HexagonProgressBar_textIsDisplayableee, true);

    // / textIsDisplayable = false;

    mTypedArray.recycle();
  }

  private float cognition1; // 认知能力
  private float fine_motion1; // 精细活动
  private float social1; // 社交
  private float self_caring1; // 自理
  private float muscle1; // 大肌肉运动
  private float language1; // 语言

  private boolean isUpdate;

  public void updateScore(float cognition, float fine_motion, float social,
      float self_caring, float muscle, float language) {
    cognition1 = cognition;
    fine_motion1 = fine_motion;
    social1 = social;
    self_caring1 = self_caring;
    muscle1 = muscle;
    language1 = language;

    isUpdate = true;

    invalidate();
  }

  private int RED_CIRCLE_RADIUS = 10;

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //
    // if (null == mPaint2) {
    // mPaint2 = new Paint();
    // mPaint2.setAntiAlias(true);
    // mPaint2.setStyle(Style.FILL);
    // // paint.setColor(Color.BLACK);
    // mPaint2.setColor(Color.GRAY);
    // mPaint2.setAlpha(100);
    // }

    /**
     * 画外六边形
     */
    int centre = getWidth() / 2;// 中心坐标
    int radius = (int) (centre - roundWidth / 2);// 六边形边长

    int stepRadius = radius / 5;
    mPaint.setColor(roundColor); // 设置圆环的颜色
    mPaint.setStyle(Style.STROKE); // 设置空心
    mPaint.setStrokeWidth(roundWidth); // 设置圆环的宽度
    mPaint.setAntiAlias(true); // 消除锯齿
    mPath = new Path();// 设置路径
    // 第一个点坐标(centre-radius, getHeight()/2)
    // 第二个点坐标(centre-radius/2,getHeight()/2-Math.sqrt(3)*radius/2)
    // 第三个点坐标(centre+radius/2,getHeight()/2-Math.sqrt(3)*radius/2)
    // 第四个点坐标(centre+radius,getHeight()/2)
    // 第五个点坐标 (centre+radius/2,Math.sqrt(3)*radius/2+getHeight()/2)
    // 第六个点坐标 (centre-radius/2,Math.sqrt(3)*radius/2+getHeight()/2)
    // mPath.moveTo(centre - radius, centre); // A

    for (int k = 0; k < 5; k++) {
      mPath.moveTo(
          (float) (centre - Math.sqrt(3) * (stepRadius * (k + 1)) / 2), centre
              - (stepRadius * (k + 1)) / 2);
      mPath.lineTo(centre, centre - (stepRadius * (k + 1)));// B
      mPath.lineTo(
          (float) (centre + Math.sqrt(3) * (stepRadius * (k + 1)) / 2), centre
              - (stepRadius * (k + 1)) / 2);

      mPath.lineTo(
          (float) (centre + Math.sqrt(3) * (stepRadius * (k + 1)) / 2), centre
              + (stepRadius * (k + 1)) / 2);
      mPath.lineTo((centre), centre + (stepRadius * (k + 1)));
      mPath.lineTo(
          (float) (centre - Math.sqrt(3) * (stepRadius * (k + 1)) / 2), centre
              + (stepRadius * (k + 1)) / 2);

      mPath.close();
      canvas.drawPath(mPath, mPaint);
    }

    // 画线
    mPath.moveTo((float) (centre - Math.sqrt(3) * (stepRadius * (1)) / 2),
        centre - (stepRadius * (1)) / 2);
    mPath.lineTo((float) (centre - Math.sqrt(3) * (stepRadius * (5)) / 2),
        centre - (stepRadius * (5)) / 2);// B

    mPath.moveTo(centre, centre - (stepRadius * (0 + 1)));
    mPath.lineTo(centre, centre - (stepRadius * (4 + 1)));// B

    mPath.moveTo((float) (centre + Math.sqrt(3) * (stepRadius * (0 + 1)) / 2),
        centre - (stepRadius * (0 + 1)) / 2);
    mPath.lineTo((float) (centre + Math.sqrt(3) * (stepRadius * (4 + 1)) / 2),
        centre - (stepRadius * (4 + 1)) / 2);

    mPath.moveTo((float) (centre + Math.sqrt(3) * (stepRadius * (0 + 1)) / 2),
        centre + (stepRadius * (0 + 1)) / 2);

    mPath.lineTo((float) (centre + Math.sqrt(3) * (stepRadius * (4 + 1)) / 2),
        centre + (stepRadius * (4 + 1)) / 2);

    mPath.moveTo((centre), centre + (stepRadius * (0 + 1)));
    mPath.lineTo((centre), centre + (stepRadius * (4 + 1)));

    mPath.moveTo((float) (centre - Math.sqrt(3) * (stepRadius * (0 + 1)) / 2),
        centre + (stepRadius * (0 + 1)) / 2);

    mPath.lineTo((float) (centre - Math.sqrt(3) * (stepRadius * (4 + 1)) / 2),
        centre + (stepRadius * (4 + 1)) / 2);

    mPath.close();
    canvas.drawPath(mPath, mPaint);

    if (isUpdate) {
      mPath2 = new Path();
      mPaint2 = new Paint();
      mPaint2.setAntiAlias(true);
      mPaint2.setStyle(Style.FILL);
      // paint.setColor(Color.BLACK);
      mPaint2.setColor(0xffffcd1d);
      mPaint2.setAlpha(200);

      mPaint3.setAntiAlias(true);
      mPaint3.setStyle(Style.FILL);
      mPaint3.setColor(0xffec6d21);
      mPaint3.setAlpha(225);

      float cx, cy;

      cx = (float) (centre - Math.sqrt(3)
          * (stepRadius + (4 * stepRadius) * fine_motion1) / 2);
      cy = centre - (stepRadius + 4 * stepRadius * fine_motion1) / 2;
      mPath2.moveTo(cx, cy);
      canvas.drawCircle(cx, cy, RED_CIRCLE_RADIUS, mPaint3);

      cx = centre;
      cy = centre - (stepRadius + (4 * stepRadius) * language1);
      mPath2.lineTo(cx, cy);
      canvas.drawCircle(cx, cy, RED_CIRCLE_RADIUS, mPaint3);

      cx = (float) (centre + Math.sqrt(3)
          * (stepRadius + 4 * stepRadius * self_caring1) / 2);
      cy = centre - (stepRadius + 4 * stepRadius * self_caring1) / 2;
      mPath2.lineTo(cx, cy);
      canvas.drawCircle(cx, cy, RED_CIRCLE_RADIUS, mPaint3);

      cx = (float) (centre + Math.sqrt(3)
          * (stepRadius + 4 * stepRadius * muscle1) / 2);
      cy = centre + (stepRadius + 4 * stepRadius * muscle1) / 2;
      mPath2.lineTo(cx, cy);
      canvas.drawCircle(cx, cy, RED_CIRCLE_RADIUS, mPaint3);

      cx = centre;
      cy = centre + (stepRadius + 4 * stepRadius * cognition1);
      mPath2.lineTo(cx, cy);
      canvas.drawCircle(cx, cy, RED_CIRCLE_RADIUS, mPaint3);

      cx = (float) (centre - Math.sqrt(3)
          * (stepRadius + 4 * stepRadius * social1) / 2);
      cy = centre + (stepRadius + 4 * stepRadius * social1) / 2;
      mPath2.lineTo(cx, cy);
      canvas.drawCircle(cx, cy, RED_CIRCLE_RADIUS, mPaint3);

      mPath2.close();
      canvas.drawPath(mPath2, mPaint2);
    }

    /**
     * 画进度百分比
     */
    mPaint.setStrokeWidth(0);
    mPaint.setColor(textColor);
    mPaint.setTextSize(textSize);
    mPaint.setTypeface(Typeface.DEFAULT_BOLD); // 设置字体
    int percent = (int) (((float) progress / (float) max) * 100); // 中间的进度百分比，先转换成float在进行除法运算，不然都为0
    float textWidth = mPaint.measureText(percent + "%"); // 测量字体宽度，我们需要根据字体的宽度设置在圆环中间

    if (textIsDisplayable && style == STROKE) {
      // canvas.drawText(percent + "%", centre - textWidth / 2, centre +
      // textSize
      // / 2, mPaint); // 画出进度百分比
      //
      // textWidth = mPaint.measureText("语言");
      // canvas.drawText("语言", centre - radius - textWidth, centre + textSize /
      // 2,
      // mPaint); // 画出进度百分比
    }

    /**
     * 画六边形进度
     */

    // // 设置进度是实心还是空心
    // path = new Path();
    // // paint.setStrokeWidth(roundWidth); // 设置圆环的宽度
    // // paint.setColor(roundProgressColor); // 设置进度的颜色
    // // paint.setAntiAlias(true);
    //
    // double k = (progress * 6) / max;
    //
    // // paint.setStyle(Paint.Style.STROKE);
    // if (k <= 1 || k == 0) {
    // path.moveTo(centre + radius, centre);
    // path.lineTo((float) (centre + radius - k * radius / 2),
    // (float) (centre + k * radius * Math.sqrt(3) / 2));
    // } else if (k > 1 && k <= 2) {
    // path.moveTo(centre + radius, centre);
    // path.lineTo(centre + radius / 2,
    // (float) ((Math.sqrt(3) * radius / 2) + centre));
    // path.lineTo((float) (centre + 1.5 * radius - k * radius),
    // (float) (centre + 0.5 * Math.sqrt(3) * radius));
    // } else if (k > 2 && k <= 3) {
    // path.moveTo(centre + radius, centre);
    // path.lineTo(centre + radius / 2,
    // (float) ((Math.sqrt(3) * radius / 2) + centre));
    // path.lineTo(centre - radius / 2,
    // (float) ((Math.sqrt(3) * radius / 2) + centre));
    // path.lineTo((float) (centre + 0.5 * radius - 0.5 * radius * k),
    // (float) (centre + 1.5 * Math.sqrt(3) * radius - 0.5 * k * radius
    // * Math.sqrt(3)));
    // } else if (k > 3 && k <= 4) {
    // path.moveTo(centre + radius, centre);
    // path.lineTo(centre + radius / 2,
    // (float) ((Math.sqrt(3) * radius / 2) + centre));
    // path.lineTo(centre - radius / 2,
    // (float) ((Math.sqrt(3) * radius / 2) + centre));
    // path.lineTo(centre - radius, centre);
    // path.lineTo((float) (centre - radius + 0.5 * k * radius - 1.5 * radius),
    // (float) (centre - 0.5 * (k - 3) * radius * Math.sqrt(3)));
    // } else if (k > 4 && k <= 5) {
    // path.moveTo(centre + radius, centre);
    // path.lineTo(centre + radius / 2,
    // (float) ((Math.sqrt(3) * radius / 2) + centre));
    // path.lineTo(centre - radius / 2,
    // (float) ((Math.sqrt(3) * radius / 2) + centre));
    // path.lineTo(centre - radius, centre);
    // path.lineTo(centre - radius / 2, (float) (centre - Math.sqrt(3) * radius
    // / 2));
    // path.lineTo((float) ((k - 4) * radius + centre - 0.5 * radius),
    // (float) (centre - Math.sqrt(3) * radius / 2));
    // } else if (k > 5 && k < 6) {
    // path.moveTo(centre + radius, centre);
    // path.lineTo(centre + radius / 2,
    // (float) ((Math.sqrt(3) * radius / 2) + centre));
    // path.lineTo(centre - radius / 2,
    // (float) ((Math.sqrt(3) * radius / 2) + centre));
    // path.lineTo(centre - radius, centre);
    // path.lineTo(centre - radius / 2, (float) (centre - Math.sqrt(3) * radius
    // / 2));
    // path.lineTo(centre + radius / 2, (float) (centre - Math.sqrt(3) * radius
    // / 2));
    // path.lineTo((float) (centre + 0.5 * radius + 0.5 * (k - 5) * radius),
    // (float) (centre - 0.5 * Math.sqrt(3) * radius + 0.5 * Math.sqrt(3)
    // * (k - 5) * radius));
    // } else {
    // path.moveTo(centre + radius, centre);
    // path.lineTo(centre + radius / 2,
    // (float) ((Math.sqrt(3) * radius / 2) + centre));
    // path.lineTo(centre - radius / 2,
    // (float) ((Math.sqrt(3) * radius / 2) + centre));
    // path.lineTo(centre - radius, centre);
    // path.lineTo(centre - radius / 2, (float) (centre - Math.sqrt(3) * radius
    // / 2));
    // path.lineTo(centre + radius / 2, (float) (centre - Math.sqrt(3) * radius
    // / 2));
    // path.lineTo(centre + radius, centre);
    // path.close();
    // }

    // canvas.drawPath(path, paint);

  }

  public synchronized double getMax() {
    return max;
  }

  /**
   * 设置进度的最大值
   * 
   * @param max
   */
  public synchronized void setMax(int max) {
    if (max < 0) {
      throw new IllegalArgumentException("max not less than 0");
    }
    this.max = max;
  }

  /**
   * 获取进度.需要同步
   * 
   * @return
   */
  public synchronized double getProgress() {
    return progress;
  }

  /**
   * 设置进度，此为线程安全控件，由于考虑多线的问题，需要同步 刷新界面调用postInvalidate()能在非UI线程刷新
   * 
   * @param progress
   */
  public synchronized void setProgress(double progress) {
    if (progress < 0) {
      throw new IllegalArgumentException("progress not less than 0");
    }
    if (progress > max) {
      progress = max;
    }
    if (progress <= max) {
      this.progress = progress;
      postInvalidate();
    }

  }

  public int getCricleColor() {
    return roundColor;
  }

  public void setCricleColor(int cricleColor) {
    this.roundColor = cricleColor;
  }

  public int getCricleProgressColor() {
    return roundProgressColor;
  }

  public void setCricleProgressColor(int cricleProgressColor) {
    this.roundProgressColor = cricleProgressColor;
  }

  public int getTextColor() {
    return textColor;
  }

  public void setTextColor(int textColor) {
    this.textColor = textColor;
  }

  public float getTextSize() {
    return textSize;
  }

  public void setTextSize(float textSize) {
    this.textSize = textSize;
  }

  public float getRoundWidth() {
    return roundWidth;
  }

  public void setRoundWidth(float roundWidth) {
    this.roundWidth = roundWidth;
  }

}
